﻿package com.urbansquall.display {
import flash.display.*;
import flash.geom.Rectangle;

public class Scale9 extends Sprite
{
	internal var m_topLeft : Bitmap;
	internal var m_topMiddle : Bitmap;
	internal var m_topRight : Bitmap;
	
	internal var m_middleLeft : Bitmap;
	internal var m_middleMiddle : Bitmap;
	internal var m_middleRight : Bitmap;
	
	internal var m_bottomLeft : Bitmap;
	internal var m_bottomMiddle : Bitmap;
	internal var m_bottomRight : Bitmap;
	
	internal var m_scale9Data : Scale9Data;
	
	public function Scale9( a_scale9Data : Scale9Data )
	{
		m_scale9Data = a_scale9Data;
		
		m_topLeft = new Bitmap( m_scale9Data.m_topLeft );
		m_topMiddle = new Bitmap( m_scale9Data.m_topMiddle );
		m_topRight = new Bitmap( m_scale9Data.m_topRight );

		m_middleLeft = new Bitmap( m_scale9Data.m_middleLeft );
		m_middleMiddle = new Bitmap( m_scale9Data.m_middleMiddle );
		m_middleRight = new Bitmap( m_scale9Data.m_middleRight );

		m_bottomLeft = new Bitmap( m_scale9Data.m_bottomLeft );
		m_bottomMiddle = new Bitmap( m_scale9Data.m_bottomMiddle );
		m_bottomRight = new Bitmap( m_scale9Data.m_bottomRight );
		
		m_topLeft.smoothing = false;
		m_topMiddle.smoothing = false;
		m_topRight.smoothing = false;

		m_middleLeft.smoothing = false;
		m_middleMiddle.smoothing = false;
		m_middleRight.smoothing = false;

		m_bottomLeft.smoothing = false;
		m_bottomMiddle.smoothing = false;
		m_bottomRight.smoothing = false;		
		
		m_topLeft.pixelSnapping = PixelSnapping.ALWAYS;
		m_topMiddle.pixelSnapping = PixelSnapping.ALWAYS;
		m_topRight.pixelSnapping = PixelSnapping.ALWAYS;

		m_middleLeft.pixelSnapping = PixelSnapping.ALWAYS;
		m_middleMiddle.pixelSnapping = PixelSnapping.ALWAYS;
		m_middleRight.pixelSnapping = PixelSnapping.ALWAYS;

		m_bottomLeft.pixelSnapping = PixelSnapping.ALWAYS;
		m_bottomMiddle.pixelSnapping = PixelSnapping.ALWAYS;
		m_bottomRight.pixelSnapping = PixelSnapping.ALWAYS;				
		
		addChild( m_topLeft );
		addChild( m_topMiddle );
		addChild( m_topRight );
		
		addChild( m_middleLeft );
		addChild( m_middleMiddle );
		addChild( m_middleRight );

		addChild( m_bottomLeft );
		addChild( m_bottomMiddle );
		addChild( m_bottomRight );
		
		reset();
	}
	
	public function reset() : void
	{
		resize( m_topLeft.width + m_topMiddle.width + m_topRight.width, m_topLeft.height + m_middleLeft.height + m_bottomLeft.height );
	}
	
	public function resize( a_width : Number, a_height : Number ) : void
	{
		width = a_width;
		height = a_height;
	}
	
	override public function set width( a_width : Number ) : void
	{
		var middleWidth : Number = Math.ceil( a_width - m_topLeft.width - m_topRight.width );
		
		m_topMiddle.width = middleWidth;
		m_middleMiddle.width = middleWidth;
		m_bottomMiddle.width = middleWidth;
		
		m_topLeft.x = 0;
		m_topMiddle.x = m_topLeft.width;
		m_topRight.x = m_topLeft.width + m_topMiddle.width;
		
		m_middleLeft.x = 0;
		m_middleMiddle.x = m_topLeft.width;
		m_middleRight.x = m_topLeft.width + m_topMiddle.width;
				
		m_bottomLeft.x = 0;
		m_bottomMiddle.x = m_topLeft.width;
		m_bottomRight.x = m_topLeft.width + m_topMiddle.width;
	}
	
	override public function set height( a_height : Number ) : void
	{
		var middleHeight : Number = Math.ceil( a_height - m_topLeft.height - m_bottomLeft.height );
		
		m_middleLeft.height = middleHeight;
		m_middleMiddle.height = middleHeight;
		m_middleRight.height = middleHeight;
		
		m_topLeft.y = 0;
		m_topMiddle.y = 0;
		m_topRight.y = 0;
		
		m_middleLeft.y = m_topLeft.height;
		m_middleMiddle.y = m_topLeft.height;
		m_middleRight.y = m_topLeft.height;
		
		m_bottomLeft.y = m_middleLeft.y + m_middleLeft.height;
		m_bottomMiddle.y = m_middleLeft.y + m_middleLeft.height;
		m_bottomRight.y = m_middleLeft.y + m_middleLeft.height;
	}
} // c
} // p